package com.xexun.gpstracker.view;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.xexun.gpstracker.AppContext;
import com.xexun.gpstracker.channel.R;
import com.xexun.gpstracker.listener.MenuItemClickListener;
import com.xexun.gpstracker.util.ViewHolder;

public class PopupOptionMenu {
    private PopupWindow menuWindow;
    private Activity activity;
    private List<String> optionList;
    private MenuItemClickListener menuItemClickListener;

    public PopupOptionMenu(Activity activity, List<String> optionList, MenuItemClickListener menuItemClickListener) {
        this.activity = activity;
        this.optionList = optionList;
        this.menuItemClickListener = menuItemClickListener;
    }

    public PopupOptionMenu(Activity activity, String[] options, MenuItemClickListener menuItemClickListener) {
        this.activity = activity;
        this.optionList = Arrays.asList(options);
        this.menuItemClickListener = menuItemClickListener;
    }

    public void show() {
        DisplayMetrics dm = activity.getResources().getDisplayMetrics();
        int width = dm.widthPixels;
        int height = activity.getWindow().getDecorView().getMeasuredHeight();
        if (menuWindow == null) {
            LayoutInflater layoutInflater = (LayoutInflater) AppContext.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View menuView = layoutInflater.inflate(R.layout.comm_menu_opt, null);
            ListView optList = (ListView) menuView.findViewById(R.id.comm_menu_opt);
            optList.setAdapter(new OptionAdapter(activity, optionList));
            optList.setOnItemClickListener(menuItemClickListener.handler(this));
            menuWindow = new PopupWindow(menuView, width, height);
        }
        // 使其聚集
        menuWindow.setFocusable(true);
        // 设置允许在外点击消失
        menuWindow.setOutsideTouchable(true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        ColorDrawable drawable = new ColorDrawable(Color.parseColor("#33000000"));
        menuWindow.setBackgroundDrawable(drawable);
        menuWindow.showAsDropDown(activity.getWindow().getDecorView(), 0, -height);
    }

    public void hide() {
        if (menuWindow != null) {
            try {
                menuWindow.dismiss();
            } catch (Exception e) {
                //
            }
        }
    }

    private class OptionAdapter extends BaseAdapter {
        private Context context;
        private List<String> optionList;

        public OptionAdapter(Context context, List<String> optionList) {
            this.context = context;
            this.optionList = optionList;
        }

        @Override
        public int getCount() {
            return optionList.size();
        }

        @Override
        public Object getItem(int position) {
            return optionList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.comm_menu_opt_item, null);
            }
            TextView menuText = ViewHolder.get(convertView, R.id.menu_text);
            menuText.setText(optionList.get(position));
            return convertView;
        }
    }
}